"use strict";

var dataGridObj = {
    $dg: undefined,
    init: function () {
        var total = 0;
        var pageSize = 20;
        var pageNum = 1;
        if (this.$dg == undefined) {
            // 设置table选择器，用来生成datagrid
            this.$dg = $('#dataGridTable');
        }
        if (this.$dg.length > 0) {
            this.$dg.datagrid({
                // 响应式布局
                url:contextPath+'/console/sill/loadSillByPage?pageNum=1&pageSize=10',
                loadMsg: '加载中...',
                fit: true,
                columns: [[
                    {field:'sillCode',title:'阀值代码',width:'20%',align:'center'},
                    {field:'sillName',title:'阀值名称',width:'20%',align:'center'},
                    {field:'sillValue',title:'阀值值',width:'15%',align:'center'},
                    {field:'sillStatus',title:'阀值状态',width:'15%',align:'center',
                        formatter: function (value, row, index) {
                            if (value == 1) {
                                return '<span class="green b">启用</span>';
                            } else {
                                return '<span class="orange b">停用</span>';
                            }
                        }},
                    {field:'sillDesc',title:'阀值描述',width:'15%',align:'center'},
                ]],
                rownumbers: true,
                rownumberWidth: 70,
                singleSelect: true,
                emptyMsg:'暂无数据',
                pagination:true,
                pageSize:pageSize,
                pageList:[5,10,20,40,80],
                toolbar: [{
                    id:"easyuiAddBtn",
                    text: '新增',
                    iconCls: 'icon-add',
                    plain:true,
                    handler: function () {
                        dataGridObj.goAdd();
                    }
                },{
                    id:"easyuiUptBtn",
                    text: '编辑',
                    iconCls: 'icon-edit',
                    plain:true,
                    handler: function () {
                        dataGridObj.goUpdate();
                    }
                },{
                    id:"easyuiDelBtn",
                    text: '删除',
                    iconCls: 'icon-remove',
                    plain:true,
                    handler: function () {
                        dataGridObj.doDeleteNode();
                    }
                }],
                onLoadSuccess:function (data) {
                    total = dataGridObj.$dg.datagrid('getData').total;
                    var pager = dataGridObj.$dg.datagrid('getPager');
                    pager.pagination({
                        onSelectPage: function (pageNumber, size) {
                            pageSize = size;
                            pageNum = pageNumber == 0 ?1:pageNumber;
                            var url = contextPath+'/console/sill/loadSillByPage?pageSize='+pageSize+'&pageNum='+pageNum;
                            $.get(
                                url,
                                {},
                                function (data) {
                                    dataGridObj.$dg.datagrid('loadData', data);
                                },
                                'json'
                            );

                        }
                    });
                    //判断运维用户是否有读写权限，开关工具栏功能按钮
                    toolbarSwitch();
                }
            });
            // 重新赋值：获取渲染后的datagrid
            this.$dg = $('#dataGridTable');
            // 渲染成功，返回true
            return true;
        }
        return false;
    },
    // 新增，调Dialog打开方法，并且可编辑
    goAdd: function () {
        dialogObj.openAdd(contextPath + '/console/sill/add');
        $('#sillStatus').combobox('setValue', 1);
    },
    // 修改，调Dialog打开方法，并且可编辑
    goUpdate: function () {
        dialogObj.openUpdate(this.$dg.datagrid('getSelected'),contextPath + '/console/sill/update');
    },
    // 删除应用
    doDeleteNode: function () {
        // 获取选中的行
        var row = this.$dg.datagrid('getSelected');
        if (row) {
            apexLayer.Confirm("您确认删除应用吗？",
                "删除应用",
                "确认",
                "取消",
                function () {
                    Ajax({
                        url: contextPath + "/console/sill/delete",
                        isMask: true,
                        data: {
                            "id": row.id
                        },
                        success: function (data) {
                            if (data.code>0) {
                                apexLayer.Msg(data.note, 1000, function () {
                                    dataGridObj.$dg.datagrid("reload");
                                });
                            } else {
                                apexLayer.Error("删除失败:" + data.note);
                            }
                        }
                    });
                }
            );
        } else {
            apexLayer.Warn('请先选择一条记录，才能进行删除操作！');
        }
    }
};

$(function () {

    dataGridObj.init();
    dialogObj.init("add-dlg","dataForm");
    // 渲染Dialog
    $('#add-dlg').dialog({
        width: 600,
        height: 'auto',
        closed: true,
        cache: false,
        modal: true,
        shadow: false,
        buttons: [{
            text: '保存',
            iconCls: 'icon-save',
            handler: function () {
                dialogObj.submiHandler("dataGridTable");
            }
        }],
        onClose: function () {
            // 还原Dialog
            dialogObj.reset();
        }
    });
});

function searchByCondition(num,size) {
    var sillCode = $("#search_sillCode").val();
    var sillName = $("#search_sillName").val();
    var sillValue = $("#search_sillValue").val();
    var sillStatus = $("#search_sillStatus").combobox("getValue");
    var pageNum = num==null||num==undefined?1:num;
    var pageSize = size==null||size==undefined?15:size;
    var url = contextPath+'/console/sill/loadSillByPage';
    $.get(
        url,
        {
            sillCode: sillCode,
            sillName: sillName,
            sillValue: sillValue,
            sillStatus: sillStatus,
            pageNum: pageNum,
            pageSize: pageSize
        },
        function (data) {
            dataGridObj.$dg.datagrid('loadData', data);
        },
        'json'
    );
}
function resetCondition() {
    $("#searchContainer .easyui-textbox").each(function () {
        $(this).textbox('setValue', "");
    });
    $("#searchContainer .easyui-combobox").each(function () {
        $(this).combobox('setValue', "");
    });
    dataGridObj.$dg.datagrid('reload');
}
